class Admin::PrintCategoryController < Admin::BaseController

  active_scaffold :print_category do |config|
      config.label = "分类管理"
      config.create.columns = [:parent, :name, :description]
      config.columns = [:parent, :name, :description, :props]
      config.list.columns.exclude :description, :props
      list.sorting = {:name => 'ASC'}
      
      columns[:parent].label = "父分类"
      columns[:name].label = "名称"
      columns[:description].label = "描述"
      columns[:props].label = "属性"
  end
  
  auto_complete_for :print_category, :name, :limit => 20, :order => 'name ASC'
  
  protected
  def before_create_save(record)
    set_parent(record)
  end
  
  def before_update_save(record)
    set_parent(record)
  end
  
  private
  def set_parent(record)
    parnet_category_name = params[:print_category][:name]
    unless  parnet_category_name == nil 
      unless parnet_category_name.empty?
        if parnet_category_name != record.name
          parentCategory = PrintCategory.find_by_name(parnet_category_name)
          record.parent_id = parentCategory.id
        else
          # 如不删除，那么这个参数将携带在所有链接后面
          params.delete :print_category
          @record.errors.add_to_base as_("分类的父分类不能是本身")
          raise ActiveRecord::RecordInvalid, @record
        end
      else
        record.parent_id = -1
      end
    end
  end
end
